﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace MvcControlsToolkit
{
    public class Window: Container
    {
        public bool IsAutoOpen
        {
            get
            {
                if (Attributes.ContainsKey("autoOpen"))
                    return bool.Parse(Attributes["autoOpen"]);
                else return true;
            }
            set { Attributes["autoOpen"] = value.ToString().ToLower(); }
        }

        public bool IsCloseOnEscape
        {
            get
            {
                if (Attributes.ContainsKey("closeOnEscape"))
                    return bool.Parse(Attributes["closeOnEscape"]);
                else return true;
            }
            set { Attributes["closeOnEscape"] = value.ToString().ToLower(); }
        }

        public string CloseText
        {
            get{ return GetString("closeText"); }
            set { SetString("closeText", value); }
        }

        public string DialogClass
        {
            get { return GetString("dialogClass"); }
            set { SetString("dialogClass", value); }

        }

        public bool IsDraggable
        {
            get
            {
                if (Attributes.ContainsKey("draggable"))
                    return bool.Parse(Attributes["draggable"]);
                else return true;
            }
            set { Attributes["draggable"] = value.ToString().ToLower(); }
        }

        public int Height
        {
            get
            {
                if (Attributes.ContainsKey("height"))
                    return int.Parse(Attributes["height"]);
                else return 0;
            }
            set { Attributes["height"] = value.ToString().ToLower(); }
        }

        public string HideEffect
        {
            get { return GetString("hide"); }
            set { SetString("hide", value); }
        }

        public int MaxHeight
        {
            get
            {
                if (Attributes.ContainsKey("maxHeight"))
                    return int.Parse(Attributes["maxHeight"]);
                else return 0;
            }
            set { Attributes["maxHeight"] = value.ToString().ToLower(); }
        }

        public int MaxWidth
        {
            get
            {
                if (Attributes.ContainsKey("maxWidth"))
                    return int.Parse(Attributes["maxWidth"]);
                else return 0;
            }
            set { Attributes["maxWidth"] = value.ToString().ToLower(); }
        }

        public int MinHeight
        {
            get
            {
                if (Attributes.ContainsKey("minHeight"))
                    return int.Parse(Attributes["minHeight"]);
                else return 0;
            }
            set { Attributes["minHeight"] = value.ToString().ToLower(); }
        }

        public int MinWidth
        {
            get
            {
                if (Attributes.ContainsKey("minWidth"))
                    return int.Parse(Attributes["minWidth"]);
                else return 0;
            }
            set { Attributes["minWidth"] = value.ToString().ToLower(); }
        }

        public bool IsModal
        {
            get
            {
                if (Attributes.ContainsKey("modal"))
                    return bool.Parse(Attributes["modal"]);
                else return false;
            }
            set { Attributes["modal"] = value.ToString().ToLower(); }
        }

        public string Position
        {
            get { return GetString("position"); }
            set { SetString("position", value); }
        }

        public bool IsResizable
        {
            get
            {
                if (Attributes.ContainsKey("resizable"))
                    return bool.Parse(Attributes["resizable"]);
                else return false;
            }
            set { Attributes["resizable"] = value.ToString().ToLower(); }
        }

        public string ShowEffect
        {
            get { return GetString("show"); }
            set { SetString("show", value); }
        }

        public bool IsStack
        {
            get
            {
                if (Attributes.ContainsKey("stack"))
                    return bool.Parse(Attributes["stack"]);
                else return true;
            }
            set { Attributes["stack"] = value.ToString().ToLower(); }
        }

        public string Title
        {
            get { return GetString("title"); }
            set { SetString("title", value); }
        }

        public int Width
        {
            get
            {
                if (Attributes.ContainsKey("width"))
                    return int.Parse(Attributes["width"]);
                else return 300;
            }
            set { Attributes["width"] = value.ToString().ToLower(); }
        }

        public string LoadingText { get; set; }

        public string OnCreate
        {
            get { return Attributes["create"]; }
            set { Attributes["create"] = value; }
        }

        public string OnOpen
        {
            get { return Attributes["open"]; }
            set { Attributes["open"] = value; }
        }

        public string OnBeforeClose
        {
            get { return Attributes["beforeClose "]; }
            set { Attributes["beforeClose "] = value; }
        }

        public string OnFocus
        {
            get { return Attributes["focus "]; }
            set { Attributes["focus "] = value; }
        }

        public string OnDragStart
        {
            get { return Attributes["dragStart"]; }
            set { Attributes["dragStart"] = value; }
        }

        public string OnDrag
        {
            get { return Attributes["drag"]; }
            set { Attributes["drag"] = value; }
        }

        public string OnDragStop
        {
            get { return Attributes["dragStop"]; }
            set { Attributes["dragStop"] = value; }
        }

        public string OnResizeStart
        {
            get { return Attributes["resizeStart"]; }
            set { Attributes["resizeStart"] = value; }
        }

        public string OnResize
        {
            get { return Attributes["resize"]; }
            set { Attributes["resize"] = value; }
        }

        public string OnResizeStop
        {
            get { return Attributes["resizeStop"]; }
            set { Attributes["resizeStop"] = value; }
        }

        public string OnClose
        {
            get { return Attributes["close"]; }
            set { Attributes["close"] = value; }
        }

        public Window(string name): base(name)
        {
            LoadingText = "Loading...";
        }

        public Window SetContent(ContentType contentType, string name, object objectModel = null, ViewDataDictionary viewData = null)
        {
            base.SetContent(contentType, name, objectModel, viewData);

            if (ContentType == ContentType.RemoteAction)
            {
                if (Attributes.Keys.Contains("open"))
                    OnOpen = "function (event, ui) { WindowLoadingContent(event, ui); " + OnOpen + "(event,ui);}";
                else
                    OnOpen = "function (event, ui) { WindowLoadingContent(event, ui); }";
            }

            return this;
        }

        public override string GetJScript()
        {
            var result = new StringBuilder();

            foreach (string key in Attributes.Keys)
            {
                result.AppendFormat(", {0}: {1}", key, Attributes[key]);
            }

            if (result.Length > 0)
                result.Remove(0, 1);

            result.Append("});");

            result.Insert(0, "var "+Name+" = $(\"#" + Name + "\").dialog({");

            return result.ToString();
        }

        public override string GetHtml()
        {
            var result = new StringBuilder();

            switch (ContentType)
            {
                case ContentType.Static:
                    result.Append(HTMLContent);
                    break;
                case ContentType.PartialView:
                    result.Append(ControlManager.RenderPartialToString(Helper, PartialViewName, PartialViewModel, PartialViewData));
                    break;
                case ContentType.RemoteAction:
                    result.Append(LoadingText);
                    break;
            }

            if (ContentType == ContentType.RemoteAction)
                return string.Format("<div id=\"{0}\" mvc-action=\"{1}\" content-type=\"{2}\">{3}</div>", Name, Action, ContentType,result);
            else
                return string.Format("<div id=\"{0}\" content-type=\"{1}\">{2}</div>", Name, ContentType, result);
        }
    }
}
